<template>
  <div class="app-container">
    <div class="main">
      <div class="filter-container g-bgc-fill-el-input">
        <div class="filter-left">
          <el-select v-model="listQuery.founder" placeholder="所属品牌" clearable @change="comHandleFilterSelectChange"
            style="width: 136px" class="filter-item">
            <el-option v-for="(item, index) in brandOptions" :key="'br' + index" :label="item.menufacturer"
              :value="item.id" />
          </el-select>
          <!-- 问过产品，这个搜索框有问题，没有这个框，这期做完，直接删除 -->
          <!-- <el-select v-model="listQuery.founder" placeholder="反馈人" clearable @change="comHandleFilterSelectChange"
          style="width: 90px" class="filter-item">
        </el-select> -->
          <el-date-picker v-model="listQuery.date" type="daterange" @change="comHandleFilterDateChange"
            format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss" style="width: 208px" range-separator=""
            start-placeholder="开始时间" end-placeholder="结束时间">
          </el-date-picker>
          <np-input-search v-model="listQuery.keyword" placeholder="搜索反馈内容、反馈人、联系电话" style="width: 360px;"
            @all-search="comHandleFilterAllSearch" />
        </div>
        <div class="filter-right">
          <el-button type="warning" @click="handleExportList">导出</el-button>
        </div>
      </div>

      <el-table class="g-el-table" :data="list" v-loading="listLoading">
        <el-table-column label="反馈内容" prop="content" width="200PX">
          <div slot-scope="{row}">
            {{ row.content }}
          </div>
        </el-table-column>
        <el-table-column label="所属品牌" width="120px">
          <template slot-scope="{row}">
            <span>{{ row.licenseCount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="反馈人" min-width="150px">
          <template slot-scope="{row}">
            <span>{{ row.createBy }}</span>
          </template>
        </el-table-column>
        <el-table-column label="手机号" min-width="150px">
          <template slot-scope="{row}">
            <span>{{ row.contactInfo }}</span>
          </template>
        </el-table-column>
        <el-table-column label="手机型号" min-width="150px">
          <template slot-scope="{row}">
            <span>{{ row.phoneModel }}</span>
          </template>
        </el-table-column>
        <el-table-column label="反馈时间" min-width="150px">
          <template slot-scope="{row}">
            <span>{{ row.createDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="230" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{row,$index}">
            <el-button type="text" @click="handleView(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <np-pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
        @pagination="getList" />
    </div>
  </div>
</template>

<script>
import { getMenufacturers } from "@/api/family-and-users-management/index"
import { fetchList, delMenufacturer } from '@/api/feedback/list'
import { parseTime } from '@/utils'
export default {
  name: 'ComplexTable',
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,

        founder: undefined, // 创建人 没有这个字段
        phoneNum: undefined, // 品牌名称
        keyword: undefined,

        date: undefined,
        startDate: undefined,
        endDate: undefined,
      },
      brandOptions: [],

      exportListLoading: false,
    }
  },
  created() {
    this.getList()
    // 获取品牌
    getMenufacturers({ pageNum: 1, pageSize: 1000 }).then(res => {
      if (res.code === 200) {
        this.brandOptions = res.data.list || []
      }
    })
  },
  methods: {
    getList() {
      this.listLoading = true
      let pans = { ...this.listQuery }
      delete pans.date
      fetchList(this.listQuery).then(res => {
        if (res.code === 200) {
          this.list = res.data.list
          this.total = res.data.total
        }

        // Just to simulate the time of the request
        this.listLoading = false
      })
    },
    comHandleFilterAllSearch(e, type) {
      this.listQuery.pageNum = 1
      this.getList()
    },
    comHandleFilterSelectChange(e) {
      this.listQuery.pageNum = 1
      this.getList()
    },
    comHandleFilterDateChange(e) {
      this.listQuery.pageNum = 1
      this.listQuery.startDate = e ? e[0] : ''
      this.listQuery.endDate = e ? e[1] : ''
      this.getList()
    },
    handleExportList() {
      this.exportListLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['反馈内容', '所属品牌', '反馈人', '手机号', '手机型号', '反馈时间']
        const filterVal = ['content', '', 'createBy', 'phone', 'phoneModel', 'createDate']
        const list = this.list
        const data = this.formatJson(filterVal, list);
        const filename = `建议反馈列表_${parseTime(new Date(), '{y}{m}{d}{h}{i}{s}')}`
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename,
          // autoWidth: this.autoWidth,
          // bookType: this.bookType
        })
        this.exportListLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    handleView(row) {
      this.$router.push({
        path: `/feedback/details`,
        query: {
          id: row.id || '',
        }
      })
    },
  }
}
</script>
<style scoped lang="scss">
.app-container {
  height: 100%;
  overflow: auto;
}

.main {
  background-color: #fff;
  border-radius: 16px;
  padding: 20px 20px 28px;
}
</style>